package com.example.cirviewtest;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Created by wangjitao on 2016/10/14 0014.
 * 自定义View的几个步骤
 * 1，自定义View属性
 * 2，在View中获得我们的自定义的属性
 * 3，重写onMeasure
 * 4,重写onDraw
 * 5，重写onLayout（这个决定view放置在哪儿）
 */
public class MyView extends View {
    private int mFirstColor;//第一圈颜色
    private int mSecondColor;//第二圈颜色
    private int mCircleWidth;//圆的宽度
    private Paint mPaint;//画笔
    private int mProgress;//当前圆弧度数
    private int mSpeed;//速度
    private boolean isNext = false;//是否应该开始下一个

    public MyView(Context context) {
//        super(context);
        this(context,null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
//        super(context, attrs);
        this(context,attrs,0);
    }

    /**
     * 必要的初始化，获得一些自定义的值
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //我们需要在构造方法中获取自己定义的属性和默认值，先获取到TypedArray,然后在通过TypedArray 获取到定义的各个属性。
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomProgressBar,defStyleAttr,0);
        int n = a.getIndexCount();//索引值

        for (int i=0;i<n;i++){//获取属性值
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.CustomProgressBar_firstColor:
                    mFirstColor = a.getColor(attr, Color.GREEN);//第一圈颜色绿色
                    break;
                case R.styleable.CustomProgressBar_secondColor:
                    mSecondColor = a.getColor(attr, Color.RED);//第2圈颜色绿色
                    break;
                case R.styleable.CustomProgressBar_circleWidth:
                    mCircleWidth = a.getDimensionPixelSize(attr,(int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics()//圆宽度
                    ));
                    break;
                case R.styleable.CustomProgressBar_speed:
                    mSpeed = a.getInt(attr, 20);//默认20速度
                    break;
            }
        }
        a.recycle();
        mPaint = new Paint();//初始化画笔
        new Thread(){//绘图线程
            public void run(){
                while (true){
                    mProgress++;
                    if (mProgress == 360){
                        mProgress =0;
                        if (!isNext)
                            isNext = true;
                        else
                            isNext = false;
                    }
                    postInvalidate();//刷新的作用
                    try {
                        Thread.sleep(mSpeed);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
            };
        }.start();

    }

    @Override
    protected void onDraw(Canvas canvas){
        int centre = getWidth() / 2;//获取圆心的x坐标
        int radius = centre - mCircleWidth / 2;//半径
        mPaint.setStrokeWidth(mCircleWidth);//设置远环的宽度
        mPaint.setAntiAlias(true);//消除锯齿
        mPaint.setStyle(Paint.Style.STROKE);//设置空心
        //用于定义的圆弧的形状和大小的界限
        RectF oval = new RectF(centre - radius, centre - radius,centre + radius,centre + radius);
        if (!isNext){//第一颜色的圈完整，第二颜色跑
            mPaint.setColor(mFirstColor);//设置圆环的颜色
            canvas.drawCircle(centre, centre, radius, mPaint);//画出圆环
            mPaint.setColor(mSecondColor);//设置圆环的颜色
            canvas.drawArc(oval, -90, mProgress, false, mPaint);//根据进度画圆弧
        }else {
            mPaint.setColor(mSecondColor);//设置圆环的颜色
            canvas.drawCircle(centre, centre, radius, mPaint);//画出圆环
            mPaint.setColor(mFirstColor);//设置圆环的颜色
            canvas.drawArc(oval, -90, mProgress, false, mPaint);//根据进度画出圆弧
        }

    }

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_my_view);
//    }


}